/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action.request;

import static com.opensymphony.xwork2.Action.SUCCESS;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import database.ProductDAO;
import database.RequestDAO;
import database.StaffAuthenticationDAO;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import model.Product;
import model.Request;
import model.Staff;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author Trang
 */
@Namespace("/Administration/Member")
public class RequestAction extends ActionSupport{
    private Staff staff;
    private String date;
    private Integer pid;
    private Product product;
    private Request request;
    private Integer rid;
    private Integer idStaff;
    private Map<Integer,String> staffs;
    private boolean result;
    private boolean approve;
    public RequestDAO requestDAO = new RequestDAO();

    public RequestAction()
    {
        staffs = new HashMap<Integer, String>();
         List<Staff> temp = new StaffAuthenticationDAO().getStaffFromRole("Store keeper");
         for(Staff item: temp)
            {
                staffs.put(item.getIdStaff(), item.getLoginName());
            }    
    }

    public Integer getIdStaff() {
        return idStaff;
    }

    public void setIdStaff(Integer idStaff) {
        this.idStaff = idStaff;
    }
    
    
    public Map<Integer, String> getStaffs() {
        return staffs;
    }

    public void setStaffs(Map<Integer, String> staffs) {
        this.staffs = staffs;
    }

    
    public boolean isResult() {
        return result;
    }

    public void setResult(boolean result) {
        this.result = result;
    }

    
    public boolean isApprove() {
        return approve;
    }

    public void setApprove(boolean approve) {
        this.approve = approve;
    }

    
    public Request getRequest() {
        return request;
    }

    public void setRequest(Request request) {
        this.request = request;
    }

    public Integer getRid() {
        return rid;
    }

    public void setRid(Integer rid) {
        this.rid = rid;
    }
    

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }
    
    

    public Integer getPid() {
        return pid;
    }

    public void setPid(Integer pid) {
        this.pid = pid;
    }
    
    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }
    
    

    public Staff getStaff() {
        return staff;
    }

    public void setStaff(Staff staff) {
        this.staff = staff;
    }
    
    
//    @Action(
//            value = "create-issuing/*",
//            results = {
//        @Result(name = "success", location = "/manageOrder/w_issueFromOrder.jsp")},
//            params = {"oid", "{1}"})
    @Action(value = "approve-submit-decision/*", results = {
        @Result(name = "success", location = "/manageRequest/w_request.jsp")},
            params = {"rid", "{1}"})
    //@SkipValidation
    public String editProfileForm() {
        System.out.println(rid);
        Map session = ActionContext.getContext().getSession();
        
//        
//        //System.out.println("Trang"+(Staff) session.get("staff"));
//        staff = (Staff) session.get("staff");
//        
//        product = (new ProductDAO().getProductFromProductId(pid));
//        Date currentdate = new Date();
//         SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
//        date = dateFormat.format(currentdate.getTime());
//        System.out.println("Staff"+date);
        request = requestDAO.getRequestByRequestId(rid);
        session.put("request", request);
        //System.out.println(request.getStaff1().getFirstname());
        return SUCCESS;
    }
    @Action(
            value="approve-submit-decision", results={
                @Result(name = "success", location ="/manageMember/w_viewRequest.jsp")
            })
    public String CatelogueManagement()
    {
        Map session = ActionContext.getContext().getSession();
        
        request = (Request) session.get("request");
        String deny;
        if(approve == true)
        deny = "approve";
                else deny = "deny";
            
        if(requestDAO.updateRequest(request.getIdRequest(), idStaff, deny))
        {
            return SUCCESS;
        }
        //requests = requestDAO.getUnprocessRequest(approver);
        else return SUCCESS;
    }
}
